

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Dragonborn-zz">
  <meta name="keywords" content="">
  
    <meta name="description" content="# 一。问题点 # 在使用 PostgreSQL 的过程中有用到数组类型与 json 类型，这些类型直接用 java 的类型去接收是会出现类型转换异常的，  怎么处理呢，这时候就要用到 TypeHandler 了 # 二。代码  java 数组类型接收 jdbc array  1234567891011121314151617181920212223242526272829303132333435">
<meta property="og:type" content="article">
<meta property="og:title" content="MyBatis之TypeHandler用法">
<meta property="og:url" content="http://example.com/2023/01/09/java/MyBatis%E4%B9%8BTypeHandler%E7%94%A8%E6%B3%95/index.html">
<meta property="og:site_name" content="Dragonborn-zz&#39;s个人博客">
<meta property="og:description" content="# 一。问题点 # 在使用 PostgreSQL 的过程中有用到数组类型与 json 类型，这些类型直接用 java 的类型去接收是会出现类型转换异常的，  怎么处理呢，这时候就要用到 TypeHandler 了 # 二。代码  java 数组类型接收 jdbc array  1234567891011121314151617181920212223242526272829303132333435">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-01-09T08:16:03.000Z">
<meta property="article:modified_time" content="2024-01-09T08:16:55.000Z">
<meta property="article:author" content="Dragonborn-zz">
<meta property="article:tag" content="java">
<meta property="article:tag" content="mybatis">
<meta property="article:tag" content="TypeHandler">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>MyBatis之TypeHandler用法 - Dragonborn-zz&#39;s个人博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.7","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Dragonborn-zz</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/" target="_self">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/" target="_self">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/" target="_self">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/" target="_self">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="https://gitee.com/dragonborn-zz" target="_self">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/home.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="MyBatis之TypeHandler用法"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-01-09 08:16" pubdate>
          2023年1月9日 早上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          2.4k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          20 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">MyBatis之TypeHandler用法</h1>
            
            
              <div class="markdown-body">
                
                <h2 id="一-问题点"><a class="markdownIt-Anchor" href="#一-问题点">#</a> 一。问题点</h2>
<h2 id="在使用postgresql的过程中有用到数组类型与json类型这些类型直接用java的类型去接收是会出现类型转换异常的怎么处理呢这时候就要用到typehandler了"><a class="markdownIt-Anchor" href="#在使用postgresql的过程中有用到数组类型与json类型这些类型直接用java的类型去接收是会出现类型转换异常的怎么处理呢这时候就要用到typehandler了">#</a> 在使用 PostgreSQL 的过程中有用到数组类型与 json 类型，这些类型直接用 java 的类型去接收是会出现类型转换异常的，<br>
 怎么处理呢，这时候就要用到 TypeHandler 了</h2>
<h2 id="二-代码"><a class="markdownIt-Anchor" href="#二-代码">#</a> 二。代码</h2>
<ul>
<li>java 数组类型接收 jdbc array</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.china315net.mybatis.handler;<br><br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.apache.ibatis.type.JdbcType;<br><span class="hljs-keyword">import</span> org.apache.ibatis.type.MappedJdbcTypes;<br><span class="hljs-keyword">import</span> org.apache.ibatis.type.MappedTypes;<br><span class="hljs-keyword">import</span> org.apache.ibatis.type.TypeException;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;<br><br><span class="hljs-keyword">import</span> java.sql.Array;<br><span class="hljs-keyword">import</span> java.sql.PreparedStatement;<br><span class="hljs-keyword">import</span> java.sql.SQLException;<br><br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 数组类型处理程序</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@MappedJdbcTypes(&#123;JdbcType.ARRAY&#125;)</span><br><span class="hljs-meta">@MappedTypes(&#123;String[].class, Short[].class, Integer[].class, Long[].class&#125;)</span><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ArrayTypeHandler</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">org</span>.apache.ibatis.type.ArrayTypeHandler &#123;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setNonNullParameter</span><span class="hljs-params">(PreparedStatement ps, <span class="hljs-type">int</span> i, Object parameter, JdbcType jdbcType)</span> <span class="hljs-keyword">throws</span> SQLException &#123;<br>        log.debug(<span class="hljs-string">&quot;jdbcType:&#123;&#125;&quot;</span>, jdbcType);<br>        <span class="hljs-keyword">if</span> (parameter <span class="hljs-keyword">instanceof</span> Array array) &#123;<br>            ps.setArray(i, array);<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-keyword">if</span> (!parameter.getClass().isArray()) &#123;<br>                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeException</span>(<span class="hljs-string">&quot;ArrayType Handler requires SQL array or java array parameter and does not support type &quot;</span> + parameter.getClass());<br>            &#125;<br>            Class&lt;?&gt; componentType = parameter.getClass().getComponentType();<br>            <span class="hljs-type">String</span> <span class="hljs-variable">arrayTypeName</span> <span class="hljs-operator">=</span> <span class="hljs-built_in">this</span>.resolveTypeName(componentType);<br>            <span class="hljs-type">Array</span> <span class="hljs-variable">array</span> <span class="hljs-operator">=</span> ps.getConnection().createArrayOf(arrayTypeName, (Object[]) parameter);<br>            ps.setArray(i, array);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<ul>
<li>java List 类型接收 jdbc array</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.china315net.mybatis.handler;<br><br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.apache.ibatis.type.JdbcType;<br><span class="hljs-keyword">import</span> org.apache.ibatis.type.MappedJdbcTypes;<br><span class="hljs-keyword">import</span> org.apache.ibatis.type.MappedTypes;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;<br><br><span class="hljs-keyword">import</span> java.sql.Array;<br><span class="hljs-keyword">import</span> java.sql.CallableStatement;<br><span class="hljs-keyword">import</span> java.sql.PreparedStatement;<br><span class="hljs-keyword">import</span> java.sql.ResultSet;<br><span class="hljs-keyword">import</span> java.sql.SQLException;<br><span class="hljs-keyword">import</span> java.util.Arrays;<br><span class="hljs-keyword">import</span> java.util.List;<br><br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 数组类型处理程序</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@MappedJdbcTypes(&#123;JdbcType.ARRAY&#125;)</span><br><span class="hljs-meta">@MappedTypes(&#123;List.class&#125;)</span><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ListToArrayTypeHandler</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">org</span>.apache.ibatis.type.ArrayTypeHandler &#123;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setNonNullParameter</span><span class="hljs-params">(PreparedStatement ps, <span class="hljs-type">int</span> i, Object parameter, JdbcType jdbcType)</span> <span class="hljs-keyword">throws</span> SQLException &#123;<br>        <span class="hljs-keyword">if</span> (parameter <span class="hljs-keyword">instanceof</span> List&lt;?&gt; list) &#123;<br>            <span class="hljs-keyword">if</span> (!list.isEmpty()) &#123;<br>                <span class="hljs-type">String</span> <span class="hljs-variable">arrayTypeName</span> <span class="hljs-operator">=</span> <span class="hljs-built_in">this</span>.resolveTypeName(list.get(<span class="hljs-number">0</span>).getClass());<br>                <span class="hljs-type">Array</span> <span class="hljs-variable">array</span> <span class="hljs-operator">=</span> ps.getConnection().createArrayOf(arrayTypeName, list.toArray(Object[]::<span class="hljs-keyword">new</span>));<br>                ps.setArray(i, array);<br>            &#125;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">getNullableResult</span><span class="hljs-params">(ResultSet rs, String columnName)</span> <span class="hljs-keyword">throws</span> SQLException &#123;<br>        log.debug(<span class="hljs-string">&quot;getNullableResult1&quot;</span>);<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.extractList(rs.getArray(columnName));<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">getNullableResult</span><span class="hljs-params">(ResultSet rs, <span class="hljs-type">int</span> columnIndex)</span> <span class="hljs-keyword">throws</span> SQLException &#123;<br>        log.debug(<span class="hljs-string">&quot;getNullableResult2&quot;</span>);<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.extractList(rs.getArray(columnIndex));<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">getNullableResult</span><span class="hljs-params">(CallableStatement cs, <span class="hljs-type">int</span> columnIndex)</span> <span class="hljs-keyword">throws</span> SQLException &#123;<br>        log.debug(<span class="hljs-string">&quot;getNullableResult3&quot;</span>);<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.extractList(cs.getArray(columnIndex));<br>    &#125;<br><br>    <span class="hljs-keyword">private</span> Object <span class="hljs-title function_">extractList</span><span class="hljs-params">(Array array)</span> <span class="hljs-keyword">throws</span> SQLException &#123;<br>        <span class="hljs-keyword">return</span> Arrays.stream(((Object[]) extractArray(array))).toList();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<ul>
<li>java 使用 FastJson2 的 JSONObject,JSONArray 类型接收 pgsql 的 json 或 jsonb</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.china315net.mybatis.handler;<br><br><span class="hljs-keyword">import</span> com.alibaba.fastjson2.JSON;<br><span class="hljs-keyword">import</span> com.alibaba.fastjson2.JSONArray;<br><span class="hljs-keyword">import</span> com.alibaba.fastjson2.JSONObject;<br><span class="hljs-keyword">import</span> org.apache.ibatis.type.BaseTypeHandler;<br><span class="hljs-keyword">import</span> org.apache.ibatis.type.JdbcType;<br><span class="hljs-keyword">import</span> org.apache.ibatis.type.MappedTypes;<br><span class="hljs-keyword">import</span> org.postgresql.util.PGobject;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;<br><br><span class="hljs-keyword">import</span> java.sql.CallableStatement;<br><span class="hljs-keyword">import</span> java.sql.PreparedStatement;<br><span class="hljs-keyword">import</span> java.sql.ResultSet;<br><span class="hljs-keyword">import</span> java.sql.SQLException;<br><br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * json类型处理程序</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@MappedTypes(&#123;JSONObject.class, JSONArray.class&#125;)</span><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">JsonbTypeHandler</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">BaseTypeHandler</span>&lt;Object&gt; &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setNonNullParameter</span><span class="hljs-params">(PreparedStatement ps, <span class="hljs-type">int</span> i, Object parameter, JdbcType jdbcType)</span> <span class="hljs-keyword">throws</span> SQLException &#123;<br>        <span class="hljs-type">PGobject</span> <span class="hljs-variable">jsonObject</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">PGobject</span>();<br>        jsonObject.setType(<span class="hljs-string">&quot;json&quot;</span>);<br>        jsonObject.setValue(JSON.toJSONString(parameter));<br>        ps.setObject(i, jsonObject);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">getNullableResult</span><span class="hljs-params">(ResultSet rs, String columnName)</span> <span class="hljs-keyword">throws</span> SQLException &#123;<br>        <span class="hljs-keyword">return</span> JSON.parse(rs.getString(columnName));<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">getNullableResult</span><span class="hljs-params">(ResultSet rs, <span class="hljs-type">int</span> columnIndex)</span> <span class="hljs-keyword">throws</span> SQLException &#123;<br>        <span class="hljs-keyword">return</span> JSON.parse(rs.getString(columnIndex));<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">getNullableResult</span><span class="hljs-params">(CallableStatement cs, <span class="hljs-type">int</span> columnIndex)</span> <span class="hljs-keyword">throws</span> SQLException &#123;<br>        <span class="hljs-keyword">return</span> JSON.parse(cs.getString(columnIndex));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<hr>
<h2 id="注意点"><a class="markdownIt-Anchor" href="#注意点">#</a> 注意点:</h2>
<h3 id="typehandler接口方法说明"><a class="markdownIt-Anchor" href="#typehandler接口方法说明">#</a> TypeHandler 接口方法说明</h3>
<ul>
<li>setNonNullParameter 方法在写入的数据库操作时会调用</li>
<li>getNullableResult 方法在查询的数据库操作时会调用</li>
</ul>
<hr>
<h3 id="类型处理器支持的java与jdbc类型"><a class="markdownIt-Anchor" href="#类型处理器支持的java与jdbc类型">#</a> 类型处理器支持的 Java 与 jdbc 类型</h3>
<ul>
<li>
<h4 id="如果要决定一个类型处理器支持哪些java类型有如下途径优先级从上到下依次增加"><a class="markdownIt-Anchor" href="#如果要决定一个类型处理器支持哪些java类型有如下途径优先级从上到下依次增加">#</a> 如果要决定一个类型处理器支持哪些<strong> Java</strong> 类型，有如下途径 (优先级从上到下依次增加)。</h4>
<ul>
<li>
<ol>
<li>类型处理器的泛型可以决定类型处理器支持的<strong> JavaType</strong>；</li>
</ol>
</li>
<li>
<ol start="2">
<li>在类型处理器上使用注解 @<strong>MappedTypes</strong> 来指定，例如 @<strong>MappedTypes({List.class})</strong>；</li>
</ol>
</li>
<li>
<ol start="3">
<li>在配置文件中注册类型处理器时，通过 &lt;<strong>typeHandler</strong> &gt; 标签的<strong> javaType</strong> 属性来指定，例如 <code>&lt;typeHandler handler=&quot;com.lee.learn.mybatis.typehandler.ListStringTypeHandler&quot; javaType=&quot;List&quot;/&gt;</code> 。</li>
</ol>
</li>
</ul>
</li>
<li>
<h4 id="如果要决定一个类型处理器支持哪些jdbc类型有如下途径优先级从上到下依次增加"><a class="markdownIt-Anchor" href="#如果要决定一个类型处理器支持哪些jdbc类型有如下途径优先级从上到下依次增加">#</a> 如果要决定一个类型处理器支持哪些<strong> Jdbc</strong> 类型，有如下途径 (优先级从上到下依次增加)。</h4>
<ul>
<li>
<ol>
<li>在类型处理器上使用注解 @<strong>MappedJdbcTypes</strong> 来指定，例如 @<strong>MappedJdbcTypes({JdbcType.VARCHAR, JdbcType.CHAR})</strong>；</li>
</ol>
</li>
<li>
<ol start="2">
<li>在配置文件中注册类型处理器时，通过 &lt;<strong>typeHandler</strong> &gt; 标签的<strong> jdbcType</strong> 属性来指定（注意：同时也需要设置了<strong> javaType</strong><br>
 属性，否则<strong> jdbcType</strong> 属性不生效）， 例如 &lt;<strong>typeHandler handler=&quot;<br>
com.lee.learn.mybatis.typehandler.ListStringTypeHandler&quot; javaType=“List” jdbcType=“VARCHAR”/</strong>&gt;。</li>
</ol>
</li>
</ul>
</li>
</ul>
<hr>
<h3 id="typehandler如何生效"><a class="markdownIt-Anchor" href="#typehandler如何生效">#</a> TypeHandler 如何生效</h3>
<p>通常，<strong>TypeHandler</strong> 的使用场景有两个。</p>
<ol>
<li>在设置预处理语句（<strong>PreparedStatement</strong>）中的参数时，完成<strong> Java</strong> 类型到<strong> Jdbc</strong> 类型的转换，通常就是<strong> INSERT</strong> 和<strong> UPDATE</strong><br>
 的场景；</li>
<li>在将查询到的结果记录映射到<strong> Java</strong> 实体对象时，完成<strong> Jdbc</strong> 类型到<strong> Java</strong> 类型的转换，通常就是会使用到 &lt;<strong>resultMap</strong><br>
 &gt; 的场景。</li>
</ol>
<p>在使用场景下，如何让我们自定义的<strong> TypeHandler</strong> 生效，如下直接给出结论，再做验证。</p>
<ol>
<li><strong>显式使用</strong>。示例中就是显示使用，即在 &lt;<strong>result</strong> &gt; 标签中和 <code>#&#123;&#125;</code>  占位符中使用<strong> typeHandler</strong><br>
 属性来指定使用的类型处理器，这种方式是最简单粗暴的，就算不在配置文件中注册类型处理器，就算没有为类型处理器配置任何支持的<br>
<strong> Jdbc</strong> 类型，只要在 &lt;<strong>result</strong> &gt; 标签中和 <code>#&#123;&#125;</code>  占位符中使用了<strong> typeHandler</strong> 属性来指定要使用的类型处理器，那么 **MyBatis<br>
** 就会使用这个类型处理器；</li>
<li><strong>隐式使用</strong>。通常，我们是不会关注到<strong> TypeHandler</strong> 的，然而大部分时候<strong> Java</strong> 类型到<strong> Jdbc</strong> 类型的相互转换都能成功完成，是因为<br>
<strong> MyBatis</strong> 会隐式使用其内置的<strong> TypeHandler</strong>，而隐式使用哪个内置<strong> TypeHandler</strong>，是通过 &lt;<strong>result</strong> &gt; 标签和 <code>#&#123;&#125;</code>  占位符的<br>
<strong> JavaType</strong> 和<strong> JdbcType</strong> 进行推断的。</li>
</ol>
<p><strong>显式使用</strong>没什么好说的，最为简单明了。下面重点说一下<strong>隐式使用</strong>。</p>
<p>首先能够被隐式使用的<strong> TypeHandler</strong>，都需要完成注册，自定义的<strong> TypeHandler</strong> 可以在配置文件中通过 &lt;<strong>typeHandler</strong><br>
 &gt; 标签注册，而内置的<strong> TypeHandler</strong> 是在<strong> TypeHandlerRegistry#TypeHandlerRegistry</strong> 方法完成的注册，这个方法有点长，这里不再展示。</p>
<p>然后每一个<strong> TypeHandler</strong> 都有其支持的<strong> Java</strong> 类型，以及可能支持的<strong> Jdbc</strong> 类型（也可能没有），<strong>TypeHandler</strong> 注册到 **MyBatis<br>
** 中后，是按照如下形式存储的。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">Map&lt;JavaType, Map&lt;JdbcType, TypeHandler&gt;&gt;<span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> Map&lt;Type, Map&lt;JdbcType, TypeHandler&lt;?&gt;&gt;&gt;typeHandlerMap=<span class="hljs-keyword">new</span> <span class="hljs-title class_">ConcurrentHashMap</span>&lt;&gt;();<br></code></pre></td></tr></table></figure>
<p>对隐式使用<strong> TypeHandler</strong> 做一个小结。</p>
<ol>
<li>每一个<strong> TypeHandler</strong> 都有其支持的<strong> Java</strong> 类型，以及可能支持的<strong> Jdbc</strong> 类型（也可能没有），并且在<strong> MyBatis</strong> 中以 *<br>
<em>Map&lt;JavaType, Map&lt;JdbcType, TypeHandler&gt;&gt;</em>* 的形式存放；</li>
<li>如果有多个<strong> TypeHandler</strong> 的支持的<strong> Java</strong> 类型和<strong> Jdbc</strong> 类型都一样，则后注册的<strong> TypeHandler</strong> 会覆盖先注册的 *<br>
<em>TypeHandler</em>*；</li>
<li>如果在<strong> MyBatis</strong> 的参数占位符 <code>#&#123;&#125;</code>  或者结果映射标签 &lt;<strong>result</strong> &gt; 中通过<strong> javaType</strong> 属性指定了<strong> JavaType</strong>，则<strong> MyBatis</strong><br>
 在推断使用哪种<strong> TypeHandler</strong> 时依据的<strong> JavaType</strong> 会使用<strong> javaType</strong> 属性的值，否则，如果是 &lt;<strong>result</strong> &gt; 的话则<strong> MyBatis</strong><br>
 能根据映射对象推断出<strong> JavaType</strong>，如果是 <code>#&#123;&#125;</code>  的话则<strong> JavaType</strong> 为<strong> Object</strong>；</li>
<li>如果在<strong> MyBatis</strong> 的参数占位符 <code>#&#123;&#125;</code>  或者结果映射标签 &lt;<strong>result</strong> &gt; 中通过<strong> jdbcType</strong> 属性指定了<strong> JdbcType</strong>，则<strong> MyBatis</strong><br>
 在推断使用哪种<strong> TypeHandler</strong> 时依据的<strong> JdbcType</strong> 会使用<strong> jdbcType</strong> 属性的值，否则依据的<strong> JdbcType</strong> 会为<strong> null</strong>；</li>
<li><strong>MyBatis</strong> 在推断使用哪个<strong> TypeHandler</strong> 时，会先使用<strong> JavaType</strong> 拿到<strong> JavaType</strong> 对应的<strong> Map&lt;JdbcType,<br>
TypeHandler&gt;</strong>，然后使用<strong> JdbcType</strong> 去匹配<strong> TypeHandler</strong>，匹配不到则再使用<strong> JdbcType=null</strong> 去匹配<strong> TypeHandler</strong><br>
，如果还匹配不到，则判断<strong> JavaType</strong> 对应的<strong> TypeHandler</strong> 是否有多个，如果是多个则返回<strong> null</strong> 表示匹配失败，如果只有一个则使用这个<br>
<strong> TypeHandler</strong>。</li>
<li>spring 框架可以把 TypeHandler 实例注册到 IOC 容器，在使用时会根据类型去推断出要用哪个类型处理器</li>
</ol>
<h2 id="最后建议自定义的typehandler都要为其指定支持的javatype和jdbctype以及必要时在result标签和占位符中都把javatype和jdbctype属性配置上这样mybatis能够快速无误的帮我们推断出应该使用哪个类型处理器"><a class="markdownIt-Anchor" href="#最后建议自定义的typehandler都要为其指定支持的javatype和jdbctype以及必要时在result标签和占位符中都把javatype和jdbctype属性配置上这样mybatis能够快速无误的帮我们推断出应该使用哪个类型处理器">#</a> 最后建议自定义的<strong> TypeHandler</strong> 都要为其指定支持的<strong> JavaType</strong> 和<strong> JdbcType</strong>，以及必要时在 &lt;<strong>result</strong> &gt; 标签和 <code>#&#123;&#125;</code>  占位符中都把<br>
<strong> javaType</strong> 和<strong> jdbcType</strong> 属性配置上，这样<strong> MyBatis</strong> 能够快速无误的帮我们推断出应该使用哪个类型处理器。</h2>
<h3 id="小补充"><a class="markdownIt-Anchor" href="#小补充">#</a> 小补充</h3>
<p>这里再对为参数占位符 <code>#&#123;&#125;</code>  推断类型处理器时的一些逻辑进行补充说明，不看也不影响对本篇文章的理解。</p>
<p>为参数占位符 <code>#&#123;&#125;</code>  推断类型处理器时，如果没有通过<strong> javaType</strong> 来指定<strong> Java</strong> 类型，那么<strong> MyBatis</strong> 是无法知道<strong> Java</strong><br>
 类型是什么的（而 &lt;<strong>result</strong> &gt; 标签是可以的，这是不同点），此时<strong> MyBatis</strong> 会默认<strong> Java</strong> 类型是<strong> Object</strong>，然后通过<strong> Object</strong> 这个<br>
<strong> JavaType</strong> 拿到一个<strong> UnknownTypeHandler</strong> 内置类型处理器，下面看一下<strong> UnknownTypeHandler</strong> 的<strong> setNonNullParameter ()</strong> 方法。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setNonNullParameter</span><span class="hljs-params">(PreparedStatement ps, <span class="hljs-type">int</span> i, Object parameter, JdbcType jdbcType)</span>         <span class="hljs-keyword">throws</span> SQLException &#123;     <span class="hljs-type">TypeHandler</span> <span class="hljs-variable">handler</span> <span class="hljs-operator">=</span> resolveTypeHandler(parameter, jdbcType);     handler.setParameter(ps, i, parameter, jdbcType); &#125;  <span class="hljs-keyword">private</span> TypeHandler&lt;?&gt; resolveTypeHandler(Object parameter, JdbcType jdbcType) &#123;     TypeHandler&lt;?&gt; handler;     <span class="hljs-keyword">if</span> (parameter == <span class="hljs-literal">null</span>) &#123;         handler = OBJECT_TYPE_HANDLER;     &#125; <span class="hljs-keyword">else</span> &#123;         <span class="hljs-comment">// 根据需要设置到PreparedStatement中的参数判断出Java类型         // 然后再调用到TypeHandlerRegistry#getTypeHandler拿TypeHandler         handler = typeHandlerRegistrySupplier.get().getTypeHandler(parameter.getClass(), jdbcType);         if (handler == null || handler instanceof UnknownTypeHandler) &#123;             handler = OBJECT_TYPE_HANDLER;         &#125;     &#125;     return handler; &#125;</span><br></code></pre></td></tr></table></figure>
<p>已知<strong> setNonNullParameter ()</strong> 方法是会在实际执行<strong> SQL</strong> 语句前被调用到，此时会完成<strong> PreparedStatement</strong> 的参数设置，因此这时能够拿到实际设置到<br>
<strong> PreparedStatement</strong> 中的参数值从而得到参数的<strong> JavaType</strong>，所以这时会再尝试基于<strong> JavaType</strong> 和<strong> JdbcType</strong> 去匹配 *<br>
<em>TypeHandler</em>*。</p>
<p>所以本质上就算没有通过<strong> javaType</strong> 指定<strong> JavaType</strong>，&lt;<strong>result</strong> &gt; 标签和 <code>#&#123;&#125;</code>  参数占位符都是能够拿到<strong> JavaType</strong>，只不过 &lt;*<br>
<em>result</em>*&gt; 标签在构建<strong> ResultMapping</strong> 时就能够拿到<strong> JavaType</strong>，而 <code>#&#123;&#125;</code>  参数占位符需要在<strong> SQL</strong> 语句实际执行前为 *<br>
<em>PreparedStatement<strong> 设置参数时才能够拿到</strong> JavaType</em>*。</p>
<p>那么按照本节的结论，为什么第四节最后的<strong>例子 1</strong> 中的 <code>#&#123;&#125;</code>  使用不到<strong> ListStringTypeHandler</strong> 呢，这是因为在为 *<br>
<em>PreparedStatement<strong> 设置参数时，<strong>studentIntention</strong> 这个参数的实际类型是</strong> ArrayList</em>*，而不是<strong> List</strong>，但在<strong> MyBatis</strong> 中，认为<br>
<strong> ListStringTypeHandler</strong> 是支持<strong> List</strong> 而不是<strong> ArrayList</strong> 的。</p>
<h2 id="总结"><a class="markdownIt-Anchor" href="#总结">#</a> 总结</h2>
<p><strong>TypeHandler</strong> 能够帮助完成<strong> Java</strong> 类型到<strong> Jdbc</strong> 类型的相互转换，对于常规的转换，<strong>MyBatis</strong> 提供了内置的<strong> TypeHandler</strong><br>
，而对于非常规的转换，需要自定义<strong> TypeHandler</strong>。自定义方式有两种，如下所示。</p>
<ol>
<li>实现<strong> TypeHandler</strong> 接口；</li>
<li>继承<strong> BaseTypeHandler</strong> 抽象类。</li>
</ol>
<p>更推荐使用继承<strong> BaseTypeHandler</strong> 抽象类的方式来自定义<strong> TypeHandler</strong>。</p>
<p>自定义的<strong> TypeHandler</strong> 有如下两种方式被使用。</p>
<ol>
<li>显示使用。在 &lt;<strong>result</strong> &gt; 标签或者 <code>#&#123;&#125;</code>  中通过<strong> typeHandler</strong> 属性指定要使用的<strong> TypeHandler</strong>；</li>
<li>隐式使用。通过 &lt;<strong>result</strong> &gt; 标签或者 <code>#&#123;&#125;</code>  的<strong> JavaType</strong> 和<strong> JdbcType</strong>，由<strong> MyBatis</strong> 推断出需要使用的<strong> TypeHandler</strong>。</li>
</ol>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E7%8E%AF%E5%A2%83%E9%83%A8%E7%BD%B2/" class="category-chain-item">环境部署</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/java/" class="print-no-link">#java</a>
      
        <a href="/tags/mybatis/" class="print-no-link">#mybatis</a>
      
        <a href="/tags/TypeHandler/" class="print-no-link">#TypeHandler</a>
      
    </div>
  
</div>


              

              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2023/02/06/%E5%B7%A5%E5%85%B7/git%E6%9B%B4%E6%96%B0.gitignore%E6%96%87%E4%BB%B6%E5%90%8E%E4%B8%8D%E7%94%9F%E6%95%88/" title="git更新.gitignore文件后不生效">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">git更新.gitignore文件后不生效</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
    <!-- 备案信息 ICP for China -->
    <div class="beian">
  <span>
    <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
      皖ICP备2023001508号
    </a>
  </span>
  
</div>

  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
